package org.jeecg.modules.core.base.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.core.base.entity.BaseCar;
import org.jeecg.modules.core.base.entity.BaseCarWeightLimit;
import org.jeecg.modules.core.base.mapper.BaseCarMapper;
import org.jeecg.modules.core.base.mapper.BaseCarWeightLimitMapper;
import org.jeecg.modules.core.base.service.IBaseCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 车辆表
 * @Author: jeecg-boot
 * @Date:   2021-07-21
 * @Version: V1.0
 */
@Service
@Slf4j
public class BaseCarServiceImpl extends ServiceImpl<BaseCarMapper, BaseCar> implements IBaseCarService {

    @Autowired
    public RedisTemplate<String, Object> redisTemplate;

    @Resource
    BaseCarMapper baseCarMapper;

    @Resource
    BaseCarWeightLimitMapper baseCarWeightLimitMapper;

    @Override
    public Map<String, BaseCar> getAllCar()  {
        String key = "getAllCarInfo";
        //添加 一个 hash集合
        HashOperations<String, String, BaseCar> hash = redisTemplate.opsForHash();
        if (redisTemplate.hasKey(key)) {
//            log.info(key + "=======缓存========过期时间==========" + redisTemplate.getExpire(key));
            return hash.entries(key);
        } else {
            //车辆及限重信息初始化 bracolor
            List<BaseCar> carList = baseCarMapper.getAllAndWeightLimit();
            //获取所有车型限重表记录
            List<BaseCarWeightLimit> axleTypeList = baseCarWeightLimitMapper.selectList(null);
            Map<Integer, Double> type = new HashMap<>(16);
            axleTypeList.forEach(axle -> {
                if (axle.getAcquiesceIn() == 1) {
                    type.put(axle.getAxesNum(), axle.getWeightLimit());
                }
            });
            Map<String, BaseCar> catWeight = new HashMap<>(16);
            carList.forEach(vo -> {
                if (vo.getWeightLimit() == null) {
                    vo.setWeightLimit(type.get(vo.getAxesNum()));
                }
                catWeight.put(vo.getVehicleNo(), vo);
            });
            hash.putAll(key, catWeight);
//            log.info(key + "=======无缓存========过期时间==========" + redisTemplate.getExpire(key));
            return catWeight;
        }

    }
}
